package com.weifly.weistock.record.asset.impl;

import com.weifly.weistock.record.asset.domain.AssetDayDto;
import com.weifly.weistock.record.asset.domain.AssetYearDto;

import java.util.*;

/**
 * assetDay遍历
 *
 * @author weifly
 * @since 2019/9/25
 */
public class AssetDayVisitor {

    private List<AssetYearDto> yearList;

    public AssetDayVisitor(Collection<AssetYearDto> years){
        this.yearList = new ArrayList<>();
        this.yearList.addAll(years);
    }

    public Iterator<AssetDayDto> previousIter(){
        return new PreIterator();
    }

    private class PreIterator<AssetDayDto> implements Iterator<AssetDayDto>{

        private ListIterator<AssetYearDto> yearIter;
        private ListIterator<AssetDayDto> dayIter;

        public PreIterator(){
            this.yearIter = yearList.listIterator(yearList.size());
        }

        @Override
        public boolean hasNext() {
            while(this.dayIter==null || !this.dayIter.hasPrevious()){
                // 获得新的dayIter
                this.dayIter = this.takeNewDayIter();
                if(this.dayIter==null){
                    return false;
                }
            }
            return true;
        }

        private ListIterator takeNewDayIter(){
            while(this.yearIter.hasPrevious()){
                AssetYearDto yearDto = this.yearIter.previous();
                if(yearDto.getDayList().size()>0){
                    return yearDto.getDayList().listIterator(yearDto.getDayList().size());
                }
            }
            return null;
        }

        @Override
        public AssetDayDto next() {
            return this.dayIter.previous();
        }
    }
}
